﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common.Extension;

namespace Qianwei.Web
{
    public partial class ProductSuit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetContent();
            }
        }

        private void SetContent()
        {
            int mincount = 4;
            //string picnumber = "<span style='color:#000;margin-left:20px;'>(<span style='color:red;'>{0}</span>/{1})<span>";
            string url = System.IO.Path.GetFileName(Request.Url.AbsolutePath);
            IList<page_info> pageinfo = PageInfoManager.Instance.QueryPageInfo(string.Format("Page_Url='{0}'", url));
            if (pageinfo == null || pageinfo.Count == 0)
            {
                lcontenttitle.Text = "未知标题";
                return;
            }
            page_info pagenew = pageinfo.First();
            //lcontenttitle.Text = pagenew.Page_Title + string.Format(picnumber, 9, 19);
            lcontenttitle.Text = pagenew.Page_Title;

            int picid = Request["pid"].TryInt();
            int action = Request["action"].TryInt();
            List<product_info> productlistorigin = ProductInfoManager.Instance.QueryProductInfo(string.Format("Product_Type={0}", (int)AppEnum.ProductType.Suit)) as List<product_info>;
            if (productlistorigin == null || productlistorigin.Count == 0)
            {
                lcontenttitle.Text = "未知标题";
                return;
            }
            var productlistsort = productlistorigin.OrderBy(s => s.Sort_Id).ToList();

            var curpic = productlistsort.Where(s => s.Product_Id == picid);
            product_info pic;
            if (curpic == null || curpic.Count() == 0)
            {
                pic = productlistsort.First();
            }
            else
            {
                pic = curpic.First();
            }
            hf_curpic.Value = "pic" + pic.Product_Id.ToString();
            lpicInfo.Text = pic.Picture_Desc;
            lphoto.Text = string.Format("<img id=\"big_photo\" alt=\"\" src=\"{0}\"/>", pic.Picture_Path);

            StringBuilder strminpic = new StringBuilder();
            strminpic.Append("<ul>");
            int piindex = productlistsort.IndexOf(pic);
            if (action == 0)
            {
                piindex -= mincount / 2;
            }
            else if (action == 1)
            {
                piindex -= mincount;
            }
            else if (action == 2)
            {
                piindex += 1;
            }
            if (piindex + mincount > productlistsort.Count) piindex = productlistsort.Count - mincount;
            if (piindex < 0) piindex = 0;

            if (piindex < productlistsort.Count)
                lrollL.Text = string.Format("<a href=\"ProductSuit.aspx?pic={0}&action=1\"></a>", productlistsort[piindex].Product_Id);
            int i = piindex;
            int listcount = 0;
            for (; listcount++ < mincount && i < productlistsort.Count; i++)
            {
                strminpic.AppendFormat("<li><a href=\"ProductSuit.aspx?pid={0}\" id=\"pic{0}\"><img height=\"93\" src=\"/PictureMin/{1}\" alt=\"\" /></a></li>",
                        productlistsort[i].Product_Id, System.IO.Path.GetFileName(productlistsort[i].Picture_Min_Path));
            }
            lrollR.Text = string.Format("<a href=\"ProductSuit.aspx?pid={0}&action=2\"></a>", productlistsort[--i].Product_Id);
            strminpic.Append("</ul>");
            lpicmin.Text = strminpic.ToString();

            var prepiclist = productlistsort.Where(s => s.Sort_Id < pic.Sort_Id);
            if (prepiclist != null && prepiclist.Count() > 0)
            {
                lBack.Text = string.Format("<a id=\"Back\" href=\"ProductSuit.aspx?pid={0}\"></a>", prepiclist.OrderByDescending(s => s.Sort_Id).First().Product_Id);
            }

            var nextpiclist = productlistsort.Where(s => s.Sort_Id > pic.Sort_Id);
            if (nextpiclist != null && nextpiclist.Count() > 0)
            {
                lNext.Text = string.Format("<a id=\"Next\" href=\"ProductSuit.aspx?pid={0}\"></a>", nextpiclist.OrderBy(s => s.Sort_Id).First().Product_Id);
            }
        }
    }
}